package com.practice.stream;

import com.practice.lambda.entity.Employee;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * 创建Stream
 * Stream三个步骤：
 * 1.创建操作
 * 2.中间操作
 * 3.终止操作
 *
 * @author hd
 * @date 2022/1/29 9:41
 */
@SpringBootTest
public class StreamAPI {
    /**
     * 创建Stream
     */
    @Test
    public void create() {
        //1. Collection 的 stream()方法、parallelStream()方法
        List<String> stringList = new ArrayList<>();
        Stream<String> stream = stringList.stream();

        //2. Arrays的静态方法
        Employee[] employees = new Employee[10];
        Stream<Employee> employeeStream = Arrays.stream(employees);

        //3. Stream的静态方法
        Stream<Employee> employeeStream1 = Stream.of(employees);
        final Stream<String> stringStream = Stream.of("aa", "bb", "cc");

        //4. Stream的迭代iterate()方法
        final Stream<Integer> integerStream = Stream.iterate(0, x -> x + 2);
        integerStream.limit(10).forEach(System.out::println);

        //5. Stream的generate()方法
        final Stream<Double> doubleStream = Stream.generate(() -> Math.random());
        doubleStream.limit(10).forEach(System.out::println);
    }

}
